/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("INTE_WORK_CENTER",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using VOL.Entity.DomainModels;
using VOL.System.IServices;
using VOL.Core.Extensions;
using VOL.Core.BaseProvider;
using VOL.Core.Enums;
using VOL.Core.Filters;
using VOL.Core.ManageUser;
using VOL.System.IRepositories;
using VOL.Core.UserManager;
using VOL.System.Repositories;

namespace VOL.System.Controllers
{
    public partial class INTE_WORK_CENTERController
    {
        private readonly IINTE_WORK_CENTERService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IINTE_WORK_CENTERRepository  _repository;

        [ActivatorUtilitiesConstructor]
        public INTE_WORK_CENTERController(
            IINTE_WORK_CENTERService service,
            IINTE_WORK_CENTERRepository repository,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _repository = repository;
            _httpContextAccessor = httpContextAccessor;
        }

        public override ActionResult GetPageData(PageDataOptions loadData)
        {
            // return base.GetPageData(loadData);
            //获取根节点数据(对应Sys_Role1.js中searchBefore方法)
            if (loadData.Value.GetInt() == 1)
            {
                return GetTreeTableRootData(loadData).Result;
            }
            // return base.GetPageData(loadData);
            return GetTreeTableRootData(loadData).Result;
        }


        /// <summary>
        /// treetable 获取子节点数据(2021.05.02)
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("getTreeTableRootData")]
        [ApiActionPermission(ActionPermissionOptions.Search)]
        public async Task<ActionResult> GetTreeTableRootData([FromBody] PageDataOptions options)
        {
            //页面加载根节点数据条件x => x.ParentId == 0,自己根据需要设置
            var query = _repository.FindAsIQueryable(x => true);
            if (UserContext.Current.IsSuperAdmin)
            {
                query = query.Where(x => x.ParentId == "0");
            }
            else
            {
                int roleId = UserContext.Current.RoleId;
                query = query.Where(x=>x.Identifier=="0");
            }
            var queryChild = _repository.FindAsIQueryable(x => true);
            var rows = await query.TakeOrderByPage(options.Page, options.Rows)
                .OrderBy(t => t.Id).Select(s => new
                {
                    s.Id,
                    s.Identifier,
                    s.WorkCenterType,
                    s.WorkCenterNameZH,
                    s.Description,
                    hasChildren=true
                }).ToListAsync();
                //.OrderBy(x => x.).Select(s => new
                //{
                //    s.Role_Id,
                //    //   ParentId=0,
                //    s.ParentId,
                //    s.RoleName,
                //    s.DeptName,
                //    s.Dept_Id,
                //    s.Enable,
                //    s.CreateDate,
                //    s.Creator,
                //    s.Modifier,
                //    s.ModifyDate,
                //    s.OrderNo,
                //    hasChildren = queryChild.Any(x => x. == s.Role_Id)
                //}).ToListAsync();
            return JsonNormal(new { total = await query.CountAsync(), rows });
        }

        /// <summary>
        ///treetable 获取子节点数据(2021.05.02)
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("getTreeTableChildrenData")]
        [ApiActionPermission(ActionPermissionOptions.Search)]
        public async Task<ActionResult> GetTreeTableChildrenData(string identifier)
        {
            //if (!UserContext.Current.IsSuperAdmin && identifier != UserContext.Current.RoleId && !RoleContext.GetAllChildren(UserContext.Current.RoleId).Any(x => x.Id == roleId))
            //{
            //    return JsonNormal(new { rows = new object[] { } });
            //}
            //点击节点时，加载子节点数据
            var roleRepository = INTE_WORK_CENTERRepository.Instance.FindAsIQueryable(x => true);
            var rows = await roleRepository.Where(x => x.ParentId == identifier)
                .Select(s => new
                {
                    s.Id,
                    s.Identifier,
                    s.WorkCenterType,
                    s.WorkCenterNameZH,
                    s.Description,
                    hasChildren = roleRepository.Any(x => x.ParentId == s.Identifier)
                }).ToListAsync();
            return JsonNormal(new { rows });
        }



    }
}
